﻿using System.Reflection.Emit;
using YAPL.Language.Compiler.CodeGen.ReflectionEmitGenerator.AstGen.Helpers;
using YAPL.Util;
using Ast = YAPL.Language.Ast;

namespace YAPL.Language.Compiler.CodeGen.ReflectionEmitGenerator.AstGen {
	public class ReCodeBlockGenerator : ReExpressionGenerator {
		public ReCodeBlockGenerator(ReCodeGenerator codeGenerator) : base(codeGenerator) {}
		public override void Generate(Ast.Expression expr) {
			var codeBlock = (Ast.CodeBlockExpression) expr;
			var ilGen = Generator.Get<ILGenerator>(ReCodeGenerator.IL_GENERATOR_KEY);
			codeBlock.Expressions.Each(blockExpr => {
				Generator.EmitExpression(blockExpr);
			if (!(blockExpr is Ast.ReturnExpression || blockExpr is Ast.FunctionDefExpression || blockExpr is Ast.ClassDefExpression))
					ilGen.Emit(OpCodes.Pop);
			});
		}
	}
}
